﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MapCreater : MonoBehaviour
{
    private GameObject _wall;
    private GameObject _barriar;
    private GameObject _water;
    private GameObject _grass;
    private GameObject _heart;
    private GameObject _invisibleWall;

    private void Awake()
    {
        LoadPrefabs();
        CreatMap();
    }

    void LoadPrefabs()
    {
        _wall = UnityEditor.AssetDatabase.LoadAssetAtPath("Assets/Prefabs/Map/Wall.prefab", typeof(GameObject)) as GameObject;
        _barriar = UnityEditor.AssetDatabase.LoadAssetAtPath("Assets/Prefabs/Map/Barrier.prefab", typeof(GameObject)) as GameObject;
        _water = UnityEditor.AssetDatabase.LoadAssetAtPath("Assets/Prefabs/Map/Water.prefab", typeof(GameObject)) as GameObject;
        _grass = UnityEditor.AssetDatabase.LoadAssetAtPath("Assets/Prefabs/Map/Grass.prefab", typeof(GameObject)) as GameObject;
        _heart = UnityEditor.AssetDatabase.LoadAssetAtPath("Assets/Prefabs/Map/Heart.prefab", typeof(GameObject)) as GameObject;
        _invisibleWall = UnityEditor.AssetDatabase.LoadAssetAtPath("Assets/Prefabs/Map/InvisibleWall.prefab", typeof(GameObject)) as GameObject;
    }


    void CreatMap()
    {
        for (int i = -11; i <= 11; i++)
        {
            for (int j = 9; j >= -9; j--)
            {
                if (i == -11 || i == 11 || j == 9 || j == -9)//地图的最外层实例化为隐形墙
                {
                    InstantiatePrefab(_invisibleWall, new Vector3(i, j, 0), transform);
                }
                else if (i==-10||i==10||j==8||j==-8)//地图的边缘一圈除了老家没有任何物体
                {
                    if (i==0&&j==-8)
                    {
                        InstantiatePrefab(_heart, new Vector3(i, j, 0), transform);
                    }
                    if ((i==-1||i==1)&&j==-8)
                    {
                        InstantiatePrefab(_wall, new Vector3(i, j, 0), transform);
                    }
                    continue;
                }
                else//其他的地方随机实例化物体
                {
                    if (j==-7)//老家被可以打爆的墙围起来
                    {
                        if (i>=-1&&i<=1)
                        {
                            InstantiatePrefab(_wall, new Vector3(i, j, 0), transform);
                            continue;
                        }
                    }
                    int num = Random.Range(0,8);
                    if (num==0)
                    {
                        InstantiatePrefab(_wall,new Vector3(i,j,0),transform);
                    }
                    else if (num==1)//白墙过多，几率减半给红墙
                    {
                        int num1 = Random.Range(0,2);
                        if (num1==0)
                        {
                            InstantiatePrefab(_barriar, new Vector3(i, j, 0), transform);
                        }
                        else
                        {
                            InstantiatePrefab(_wall, new Vector3(i, j, 0), transform);
                        }
                    }
                    else if (num==2)
                    {
                        InstantiatePrefab(_water, new Vector3(i, j, 0), transform);
                    }
                    else if (num==3)
                    {
                        InstantiatePrefab(_grass, new Vector3(i, j, 0), transform);
                    }
                    else
                    {
                        continue;
                    }
                }
            }
        }
    }

    void InstantiatePrefab(GameObject go,Vector3 pos,Transform parent)
    {
        GameObject gameobject= Instantiate(go, pos, Quaternion.identity);
        gameobject.transform.SetParent(parent);
    }

}
